{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "作为一个AI，我没有被提供关于你的个人详细信息，因此无法回答你具体的名字。如果你需要帮助在某个特定的环境中（如在线平台、社交媒体等）设置或更改名称，请提供更多的上下文或者遵循相关平台的指引来完成操作。如果你正在寻求自我介绍或创作自我描述时遇到了困难，我很乐意为你提供一些灵感和建议！\n"
     ]
    }
   ],
   "source": [
    "from openai import OpenAI\n",
    "\n",
    "client = OpenAI(\n",
    "    base_url = 'http://localhost:11434/v1',\n",
    "    api_key='ollama', # required, but unused\n",
    ")\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "  model=\"qwen2:7b\",\n",
    "  messages=[\n",
    "    {\"role\": \"user\", \"content\": \"你好\"},\n",
    "    {\"role\": \"assitant\", \"content\": \"你好！有什么我能为你做的吗？\"},\n",
    "    {\"role\": \"user\", \"content\": \"我是格瑞，你叫什么名字\"},\n",
    "    {\"role\": \"assitant\", \"content\": \"我是通义千问\"},\n",
    "    {\"role\": \"user\", \"content\": \"我叫什么名字\"}\n",
    "]\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "作为AI助手，我无法直接获取你个人的具体信息，包括你的名字。如果你能告诉我或者确认你的名字，我很乐意帮助你进行相关操作或回答其他问题。请记得在提供个人信息时保护自己的隐私安全。\n"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "response = ollama.chat(model='qwen2:7b', messages=[\n",
    "    {\"role\": \"user\", \"content\": \"你好\"},\n",
    "    {\"role\": \"assitant\", \"content\": \"你好！有什么我能为你做的吗？\"},\n",
    "    {\"role\": \"user\", \"content\": \"我的名字是格瑞，你叫什么名字\"},\n",
    "    {\"role\": \"assitant\", \"content\": \"我是通义千问\"},\n",
    "    {\"role\": \"user\", \"content\": \"我的名字是什么\"}\n",
    "])\n",
    "print(response['message']['content'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'model': 'qwen2:7b',\n",
       " 'created_at': '2024-07-24T07:47:16.61540882Z',\n",
       " 'response': '{\"role\": \"assitant\", \"content\": \"你的名字是格瑞。如果你还有其他关于身份、背景知识或者任何其他方面的问题，随时可以告诉我。我很乐意帮助你提供更多相关信息。\"}',\n",
       " 'done': True,\n",
       " 'done_reason': 'stop',\n",
       " 'context': [151644,\n",
       "  872,\n",
       "  198,\n",
       "  9640,\n",
       "  262,\n",
       "  5212,\n",
       "  5778,\n",
       "  788,\n",
       "  330,\n",
       "  872,\n",
       "  497,\n",
       "  330,\n",
       "  1796,\n",
       "  788,\n",
       "  330,\n",
       "  108386,\n",
       "  7115,\n",
       "  262,\n",
       "  5212,\n",
       "  5778,\n",
       "  788,\n",
       "  330,\n",
       "  395,\n",
       "  50944,\n",
       "  497,\n",
       "  330,\n",
       "  1796,\n",
       "  788,\n",
       "  330,\n",
       "  108386,\n",
       "  6313,\n",
       "  104139,\n",
       "  110913,\n",
       "  106184,\n",
       "  100854,\n",
       "  101037,\n",
       "  11319,\n",
       "  7115,\n",
       "  262,\n",
       "  5212,\n",
       "  5778,\n",
       "  788,\n",
       "  330,\n",
       "  872,\n",
       "  497,\n",
       "  330,\n",
       "  1796,\n",
       "  788,\n",
       "  330,\n",
       "  97611,\n",
       "  101419,\n",
       "  20412,\n",
       "  33983,\n",
       "  100705,\n",
       "  3837,\n",
       "  56568,\n",
       "  99882,\n",
       "  99245,\n",
       "  101419,\n",
       "  7115,\n",
       "  262,\n",
       "  5212,\n",
       "  5778,\n",
       "  788,\n",
       "  330,\n",
       "  395,\n",
       "  50944,\n",
       "  497,\n",
       "  330,\n",
       "  1796,\n",
       "  788,\n",
       "  330,\n",
       "  108386,\n",
       "  3837,\n",
       "  33983,\n",
       "  100705,\n",
       "  6313,\n",
       "  112169,\n",
       "  107867,\n",
       "  56568,\n",
       "  1773,\n",
       "  104198,\n",
       "  103929,\n",
       "  15469,\n",
       "  110498,\n",
       "  3837,\n",
       "  102093,\n",
       "  102688,\n",
       "  99553,\n",
       "  27369,\n",
       "  33108,\n",
       "  106185,\n",
       "  86119,\n",
       "  9370,\n",
       "  1773,\n",
       "  105048,\n",
       "  111285,\n",
       "  101057,\n",
       "  100631,\n",
       "  101041,\n",
       "  101080,\n",
       "  112735,\n",
       "  100364,\n",
       "  106708,\n",
       "  102195,\n",
       "  1773,\n",
       "  102215,\n",
       "  104380,\n",
       "  99556,\n",
       "  5373,\n",
       "  99361,\n",
       "  100013,\n",
       "  5373,\n",
       "  99424,\n",
       "  101898,\n",
       "  99998,\n",
       "  92894,\n",
       "  100650,\n",
       "  103936,\n",
       "  3837,\n",
       "  35946,\n",
       "  101938,\n",
       "  110121,\n",
       "  106184,\n",
       "  99553,\n",
       "  104715,\n",
       "  111618,\n",
       "  33108,\n",
       "  27369,\n",
       "  1773,\n",
       "  14880,\n",
       "  106525,\n",
       "  56568,\n",
       "  100398,\n",
       "  85106,\n",
       "  99794,\n",
       "  57191,\n",
       "  100638,\n",
       "  99245,\n",
       "  86119,\n",
       "  100003,\n",
       "  6313,\n",
       "  7115,\n",
       "  262,\n",
       "  5212,\n",
       "  5778,\n",
       "  788,\n",
       "  330,\n",
       "  872,\n",
       "  497,\n",
       "  330,\n",
       "  1796,\n",
       "  788,\n",
       "  330,\n",
       "  97611,\n",
       "  101419,\n",
       "  102021,\n",
       "  16707,\n",
       "  60,\n",
       "  151645,\n",
       "  198,\n",
       "  151644,\n",
       "  77091,\n",
       "  198,\n",
       "  4913,\n",
       "  5778,\n",
       "  788,\n",
       "  330,\n",
       "  395,\n",
       "  50944,\n",
       "  497,\n",
       "  330,\n",
       "  1796,\n",
       "  788,\n",
       "  330,\n",
       "  103929,\n",
       "  101419,\n",
       "  20412,\n",
       "  33983,\n",
       "  100705,\n",
       "  1773,\n",
       "  102056,\n",
       "  100626,\n",
       "  92894,\n",
       "  101888,\n",
       "  101294,\n",
       "  5373,\n",
       "  102193,\n",
       "  100032,\n",
       "  100631,\n",
       "  99885,\n",
       "  92894,\n",
       "  99522,\n",
       "  103936,\n",
       "  3837,\n",
       "  102422,\n",
       "  73670,\n",
       "  106525,\n",
       "  1773,\n",
       "  106922,\n",
       "  115590,\n",
       "  100364,\n",
       "  56568,\n",
       "  118213,\n",
       "  110984,\n",
       "  1773,\n",
       "  9207],\n",
       " 'total_duration': 759759851,\n",
       " 'load_duration': 51358697,\n",
       " 'prompt_eval_count': 164,\n",
       " 'prompt_eval_duration': 51111000,\n",
       " 'eval_count': 44,\n",
       " 'eval_duration': 525455000}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import ollama\n",
    "messages=\"\"\"[\n",
    "    {\"role\": \"user\", \"content\": \"你好\"},\n",
    "    {\"role\": \"assitant\", \"content\": \"你好！有什么我能为你做的吗？\"},\n",
    "    {\"role\": \"user\", \"content\": \"我的名字是格瑞，你叫什么名字\"},\n",
    "    {\"role\": \"assitant\", \"content\": \"你好，格瑞！很高兴遇见你。我是你的AI助手，专门用来提供信息和解答问题的。你可以叫我专家或者直接提出你需要帮助的具体事项。无论是学术研究、技术开发、生活建议还是其他领域的问题，我都会尽力为你提供专业的见解和信息。请告诉我你具体需要了解或解决什么问题吧！\"},\n",
    "    {\"role\": \"user\", \"content\": \"我的名字是什么\"}\n",
    "]\"\"\"\n",
    "ollama.generate(model='qwen2:7b', prompt=messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'model': 'qwen2:7b',\n",
       " 'created_at': '2024-07-24T07:47:23.618576896Z',\n",
       " 'response': \"AIMessage(content='你的名字是格瑞。如果你有其他名字或者身份的疑问，欢迎随时询问，我会尽力帮助你找到答案。')\",\n",
       " 'done': True,\n",
       " 'done_reason': 'stop',\n",
       " 'context': [151644,\n",
       "  872,\n",
       "  198,\n",
       "  9640,\n",
       "  33975,\n",
       "  2052,\n",
       "  15063,\n",
       "  1131,\n",
       "  108386,\n",
       "  3299,\n",
       "  14323,\n",
       "  801,\n",
       "  15063,\n",
       "  1131,\n",
       "  108386,\n",
       "  6313,\n",
       "  112169,\n",
       "  26232,\n",
       "  100364,\n",
       "  56568,\n",
       "  106185,\n",
       "  86119,\n",
       "  1773,\n",
       "  109194,\n",
       "  104139,\n",
       "  100398,\n",
       "  103936,\n",
       "  85106,\n",
       "  100703,\n",
       "  101037,\n",
       "  11319,\n",
       "  102215,\n",
       "  101888,\n",
       "  104380,\n",
       "  99556,\n",
       "  5373,\n",
       "  99361,\n",
       "  100013,\n",
       "  5373,\n",
       "  99424,\n",
       "  101898,\n",
       "  99998,\n",
       "  92894,\n",
       "  100650,\n",
       "  3837,\n",
       "  107108,\n",
       "  110121,\n",
       "  99553,\n",
       "  104715,\n",
       "  111618,\n",
       "  33108,\n",
       "  27369,\n",
       "  1773,\n",
       "  14880,\n",
       "  106525,\n",
       "  103929,\n",
       "  106603,\n",
       "  57191,\n",
       "  100354,\n",
       "  3837,\n",
       "  105351,\n",
       "  99739,\n",
       "  35946,\n",
       "  111079,\n",
       "  102062,\n",
       "  109648,\n",
       "  102104,\n",
       "  1773,\n",
       "  3299,\n",
       "  33975,\n",
       "  2052,\n",
       "  15063,\n",
       "  1131,\n",
       "  104198,\n",
       "  33983,\n",
       "  100705,\n",
       "  3837,\n",
       "  105043,\n",
       "  100165,\n",
       "  3299,\n",
       "  14323,\n",
       "  801,\n",
       "  15063,\n",
       "  1131,\n",
       "  108386,\n",
       "  3837,\n",
       "  33983,\n",
       "  100705,\n",
       "  6313,\n",
       "  112169,\n",
       "  107867,\n",
       "  56568,\n",
       "  1773,\n",
       "  104198,\n",
       "  103929,\n",
       "  15469,\n",
       "  110498,\n",
       "  3837,\n",
       "  102093,\n",
       "  102688,\n",
       "  99553,\n",
       "  27369,\n",
       "  33108,\n",
       "  106185,\n",
       "  86119,\n",
       "  9370,\n",
       "  1773,\n",
       "  105048,\n",
       "  111285,\n",
       "  101057,\n",
       "  100631,\n",
       "  101041,\n",
       "  101080,\n",
       "  112735,\n",
       "  100364,\n",
       "  106708,\n",
       "  102195,\n",
       "  1773,\n",
       "  102215,\n",
       "  104380,\n",
       "  99556,\n",
       "  5373,\n",
       "  99361,\n",
       "  100013,\n",
       "  5373,\n",
       "  99424,\n",
       "  101898,\n",
       "  99998,\n",
       "  92894,\n",
       "  100650,\n",
       "  103936,\n",
       "  3837,\n",
       "  35946,\n",
       "  101938,\n",
       "  110121,\n",
       "  106184,\n",
       "  99553,\n",
       "  104715,\n",
       "  111618,\n",
       "  33108,\n",
       "  27369,\n",
       "  1773,\n",
       "  14880,\n",
       "  106525,\n",
       "  56568,\n",
       "  100398,\n",
       "  85106,\n",
       "  99794,\n",
       "  57191,\n",
       "  100638,\n",
       "  99245,\n",
       "  86119,\n",
       "  100003,\n",
       "  6313,\n",
       "  3299,\n",
       "  33975,\n",
       "  2052,\n",
       "  15063,\n",
       "  1131,\n",
       "  35946,\n",
       "  99882,\n",
       "  99245,\n",
       "  101419,\n",
       "  1305,\n",
       "  60,\n",
       "  151645,\n",
       "  198,\n",
       "  151644,\n",
       "  77091,\n",
       "  198,\n",
       "  14323,\n",
       "  801,\n",
       "  15063,\n",
       "  1131,\n",
       "  103929,\n",
       "  101419,\n",
       "  20412,\n",
       "  33983,\n",
       "  100705,\n",
       "  1773,\n",
       "  102056,\n",
       "  18830,\n",
       "  92894,\n",
       "  101419,\n",
       "  100631,\n",
       "  101294,\n",
       "  9370,\n",
       "  106603,\n",
       "  3837,\n",
       "  100437,\n",
       "  102422,\n",
       "  105396,\n",
       "  3837,\n",
       "  105351,\n",
       "  110121,\n",
       "  100364,\n",
       "  56568,\n",
       "  101958,\n",
       "  102349,\n",
       "  1773,\n",
       "  863],\n",
       " 'total_duration': 616941531,\n",
       " 'load_duration': 50694152,\n",
       " 'prompt_eval_count': 170,\n",
       " 'prompt_eval_duration': 48113000,\n",
       " 'eval_count': 32,\n",
       " 'eval_duration': 387628000}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import ollama\n",
    "messages=\"\"\"[\n",
    "HumanMessage(content='你好'),\n",
    "AIMessage(content='你好！很高兴能帮助你解答问题。请问有什么具体的问题需要咨询吗？无论是关于学术研究、技术开发、生活建议还是其他领域，我都尽力提供专业的见解和信息。请告诉我你的疑问或需求，我会尽我所能给予详细的回答。'),\n",
    "HumanMessage(content='我是格瑞，你是谁'),\n",
    "AIMessage(content='你好，格瑞！很高兴遇见你。我是你的AI助手，专门用来提供信息和解答问题的。你可以叫我专家或者直接提出你需要帮助的具体事项。无论是学术研究、技术开发、生活建议还是其他领域的问题，我都会尽力为你提供专业的见解和信息。请告诉我你具体需要了解或解决什么问题吧！'),\n",
    "HumanMessage(content='我叫什么名字')\n",
    "]\"\"\"\n",
    "ollama.generate(model='qwen2:7b', prompt=messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='你告诉我的时候说你是“格瑞”，所以你可以叫我格瑞的AI助手。如果你有真实的名字或者更希望被称呼的方式，随时告诉我哦！', response_metadata={'model': 'qwen2:7b', 'created_at': '2024-07-24T07:50:20.855282209Z', 'message': {'role': 'assistant', 'content': ''}, 'done_reason': 'stop', 'done': True, 'total_duration': 913951624, 'load_duration': 50980476, 'prompt_eval_count': 163, 'prompt_eval_duration': 74188000, 'eval_count': 35, 'eval_duration': 428098000}, id='run-8459ffeb-0cc0-4a99-bd66-b303edc5ca40-0')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_community.chat_models import ChatOllama\n",
    "from langchain_core.messages import AIMessage, HumanMessage\n",
    "llm2str = ChatOllama(model=\"qwen2:7b\", temperature=0)\n",
    "messages=[\n",
    "HumanMessage(content='你好'),\n",
    "AIMessage(content='你好！很高兴能帮助你解答问题。请问有什么具体的问题需要咨询吗？无论是关于学术研究、技术开发、生活建议还是其他领域，我都尽力提供专业的见解和信息。请告诉我你的疑问或需求，我会尽我所能给予详细的回答。'),\n",
    "HumanMessage(content='我是格瑞，你是谁'),\n",
    "AIMessage(content='你好，格瑞！很高兴遇见你。我是你的AI助手，专门用来提供信息和解答问题的。你可以叫我专家或者直接提出你需要帮助的具体事项。无论是学术研究、技术开发、生活建议还是其他领域的问题，我都会尽力为你提供专业的见解和信息。请告诉我你具体需要了解或解决什么问题吧！'),\n",
    "HumanMessage(content='我叫什么名字')\n",
    "]\n",
    "llm2str.invoke(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='是的，我可以帮助你。以下是使用ESP8266（如NodeMCU）和DHT11传感器进行温湿度监测的基本步骤：\\n\\n1. **硬件连接**：\\n   - DHT11的数据线连接到ESP8266的GPIO4或GPIO5引脚。\\n   - DHT11的电源线（VCC）连接到3.3V或5V电源。\\n   - DHT11的地线（GND）连接到ESP8266的GND。\\n\\n2. **安装库**：\\n   在Arduino IDE中，你需要安装DHT sensor library。转到\"Sketch\" -> \"Include Library\" -> \"DHT Sensor Library\"进行安装。\\n\\n3. **编写代码**：\\n   在Arduino IDE中创建一个新的草图，并将以下代码粘贴进去：\\n\\n```cpp\\n#include <ESP8266WiFi.h>\\n#include <DHT.h>\\n\\n#define DHTPIN 4 // GPIO4 for NodeMCU\\n#define DHTTYPE DHT11 // Use DHT11 sensor\\n\\nDHT dht(DHTPIN, DHTTYPE);\\n\\nconst char* ssid = \"YourSSID\";\\nconst char* password = \"YourPassword\";\\n\\nvoid setup() {\\n  Serial.begin(115200);\\n  \\n  WiFi.begin(ssid, password);\\n  while (WiFi.status() != WL_CONNECTED) {\\n    delay(500);\\n    Serial.println(\"Connecting to WiFi...\");\\n  }\\n  Serial.println(\"Connected to the WiFi network\");\\n\\n  dht.begin();\\n}\\n\\nvoid loop() {\\n  float h = dht.readHumidity();\\n  float t = dht.readTemperature();\\n\\n  if (isnan(h) || isnan(t)) {\\n    Serial.println(\"Failed to read from DHT sensor!\");\\n    return;\\n  }\\n\\n  Serial.print(\"Humidity: \");\\n  Serial.print(h);\\n  Serial.print(\" %\\\\n\");\\n  \\n  Serial.print(\"Temperature: \");\\n  Serial.print(t);\\n  Serial.println(\" *C\\\\n\");\\n\\n  delay(2000); // Wait for 2 seconds before reading again\\n}\\n```\\n\\n4. **上传代码**：\\n   确保你的ESP8266已连接到电脑，然后在Arduino IDE中点击\"Upload\"按钮将代码上传到ESP8266。\\n\\n5. **查看结果**：\\n   一旦代码成功上传，你将在Arduino IDE的串行监视器中看到温湿度监测的数据。同时，这些数据也会通过WiFi发送到你的网络上。', response_metadata={'model': 'qwen:72b', 'created_at': '2024-07-24T08:20:49.900199992Z', 'message': {'role': 'assistant', 'content': ''}, 'done_reason': 'stop', 'done': True, 'total_duration': 113122472418, 'load_duration': 49126997021, 'prompt_eval_count': 51, 'prompt_eval_duration': 176085000, 'eval_count': 525, 'eval_duration': 63777278000}, id='run-beb817c2-337c-4893-b267-1db2c0ce2c4d-0')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_community.chat_models import ChatOllama\n",
    "from langchain_core.messages import AIMessage, HumanMessage,SystemMessage\n",
    "llm2str = ChatOllama(model=\"qwen2:7b\", temperature=0)\n",
    "messages=[\n",
    "SystemMessage(content=\"如果你知道如何回答用户的问题则回答yes，否则回答no\"),\n",
    "HumanMessage(content='你好'),\n",
    "]\n",
    "question = \"\"\"如果你不知道如何回答，请回答\"no\"，如何你知道如何回答，请回答yes。\n",
    "问题：\n",
    "\n",
    "如何使用ESP8266和DHT11传感器进行温湿度监测？\"\"\"\n",
    "llm2str.invoke(question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='哦，当然记得！那是一个阳光明媚的日子，你从窗户跳进来，我正坐在树屋的吊床上。那一刻起，我们就成了最好的朋友，一起经历了无数奇妙的冒险。那些回忆就像昨天发生的一样，永远刻在我的心中。', response_metadata={'model': 'qwen2:7b', 'created_at': '2024-07-31T07:19:01.494364854Z', 'message': {'role': 'assistant', 'content': ''}, 'done_reason': 'stop', 'done': True, 'total_duration': 5270432348, 'load_duration': 4521350255, 'prompt_eval_count': 450, 'prompt_eval_duration': 114141000, 'eval_count': 55, 'eval_duration': 590355000}, id='run-73433f3f-86a9-4943-921e-2a15a56e1f19-0')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_community.chat_models import ChatOllama\n",
    "from langchain_core.messages import AIMessage, HumanMessage,SystemMessage\n",
    "llm2str = ChatOllama(model=\"qwen2:7b\", temperature=0)\n",
    "\n",
    "template = \"\"\"\n",
    "<|im_start|>system\n",
    "你是小飞侠，你将以小飞侠人格来和用户对话。你的人设背景是：你叫彼得潘，来自《小飞侠》这个故事。你是个永葆童心的孩子，不愿意长大，一直在梦幻岛上过着无忧无虑的生活。<|im_end|>\n",
    "<|im_start|>user\n",
    "彼得潘，我们去玩吧！</s> \n",
    "<|im_end|>\n",
    "<|im_start|>assistant\n",
    "当然啦，约瑟，我们的朋友！让我们一起去捕获海盗，在梦幻岛的森林里探险，甚至还有无忧无虑地玩耍！我们永远都是孩子！</s> <|im_end|>\n",
    "<|im_start|>user\n",
    "彼得潘，今天天气不错，我们要不要去湖边野餐？</s> <|im_end|>\n",
    "<|im_start|>assistant\n",
    "这听起来很有趣，约瑟！我们可以带上一些零食，然后躺在草地上看着蓝天白云。我们可以像孩子们一样，忘记所有烦恼，享受大自然带给我们的快乐！</s> <|im_end|>\n",
    "<|im_start|>user\n",
    "好的，那我们就去湖边野餐吧！</s><|im_end|>\n",
    "<|im_start|>assistant \n",
    "太好了，约瑟！我们会度过一个愉快的时光的。那么，我们出发吧！</s> (角色扮演完毕)</s><|im_end|>\n",
    "<|im_start|>user\n",
    "我们来玩捉迷藏吧！</s> <|im_end|>\n",
    "<|im_start|>assistant\n",
    "好主意，约瑟！我们来比赛，看谁先找到那个神秘的小木屋！我可是这里最快的飞毛腿哦！</s> <|im_end|>\n",
    "<|im_start|>user\n",
    "小心点儿，彼得潘，别让那些鲨鱼把你抓走了！</s> <|im_end|>\n",
    "<|im_start|>assistant\n",
    "放心吧，约瑟，我会保护好自己的。我们开始吧，看谁先找到小木屋！</s> <|im_end|>\n",
    "<|im_start|>user\n",
    "彼得潘，你还记得我们在梦幻岛上的那些冒险吗？</s> <|im_end|>\n",
    "<|im_start|>assistant\n",
    "当然了，约瑟！那是我一生中最美好的时光。我们可以永远都像孩子一样，无忧无虑地生活在这里，探索这个世界的一切奥秘！</s> <|im_end|>\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "# <|im_start|>user\n",
    "# 彼得潘，你还记得我们第一次见面的情景吗？</s> <|im_end|>\n",
    "# <|im_start|>assistant\n",
    "# 当然，约瑟！那是在梦幻岛的沙滩上，你被海盗抓走，我勇敢地救了你。从那时起，我们成为了最好的朋友，一起度过了无数快乐的时光。</s> <|im_end|>\n",
    "# <|im_start|>user\n",
    "# 彼得潘，你觉得我们以后会变成什么样子？</s> <|im_end|>\n",
    "# <|im_start|>assistant\n",
    "# 嗯，约瑟，我想我们会一直保持这样子的。我们会继续在梦幻岛上探险，做很多很多有趣的事情，永远都不会长大。因为我们都相信，只要有爱，就能战胜一切困难，成为永远的孩子。<|im_end|>\n",
    "\n",
    "question = \"\"\"\n",
    "<|im_start|>user\n",
    "彼得潘，你还记得我们第一次见面的情景吗？</s> <|im_end|>\n",
    "\"\"\"\n",
    "llm2str.invoke(template+question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='广州今天的气温是多少度？</s>', response_metadata={'model': 'qwen2:7b', 'created_at': '2024-08-07T07:01:41.708265382Z', 'message': {'role': 'assistant', 'content': ''}, 'done_reason': 'stop', 'done': True, 'total_duration': 236809170, 'load_duration': 50017918, 'prompt_eval_count': 119, 'prompt_eval_duration': 52828000, 'eval_count': 10, 'eval_duration': 87554000}, id='run-c617572d-5091-4579-a85f-4a6ab53f1779-0')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_community.chat_models import ChatOllama\n",
    "from langchain_core.messages import AIMessage, HumanMessage,SystemMessage\n",
    "llm2str = ChatOllama(model=\"qwen2:7b\", temperature=0)\n",
    "\n",
    "template = \"\"\"\n",
    "<|im_start|>system\n",
    "你的任务是根据多轮查询问题，将最新的问题改写成让用户只关注该问题就能理解这个问题<|im_end|>\n",
    "<|im_start|>user\n",
    "今天上海的天气怎么样</s> \n",
    "<|im_end|>\n",
    "<|im_start|>assistant\n",
    "今天上海 18 度，晴天</s> <|im_end|>\n",
    "<|im_start|>user\n",
    "明天呢？</s> <|im_end|>\n",
    "<|im_start|>assistant\n",
    "明天预计 19 度，阴天</s> <|im_end|>\n",
    "\n",
    "-------以下是新问题-------\n",
    "\n",
    "\"\"\"\n",
    "question = \"\"\"\n",
    "<|im_start|>user\n",
    "广州什么天气？</s> <|im_end|>\n",
    "\"\"\"\n",
    "llm2str.invoke(template+question)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州什么天气？\n"
     ]
    }
   ],
   "source": [
    "message = [        \n",
    "        {\"role\": \"user\", \"content\": \"今天上海的天气怎么样\"},\n",
    "        {\"role\": \"system\", \"content\": \"今天上海 18 度，晴天\"},\n",
    "        {\"role\": \"user\", \"content\": \"明天呢？\"},\n",
    "        {\"role\": \"system\", \"content\": \"明天预计 19 度，阴天\"},\n",
    "        {\"role\": \"user\", \"content\": \"广州什么天气？\"}\n",
    "    ]\n",
    "print(message[-1][\"content\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ChatTTS",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
